
window.addEventListener("load", function () {
    // 用户账号开头必须是字母，长度在6~12位之间，并且账号中只能出现字母、数字、下划线，其他字符不允许出现
    var regusername = /^[a-zA-Z]{1,}\w{5,11}$/g;
    // 用户密码必须是6~12位字符组成，两次输入密码必须一致
    var reguserpass = /^\S{6,12}$/g;
    // 电子邮箱必须合法，只能是126/163/qq邮箱中的一种，并且是以.com结尾
    var ruserpass = document.querySelector("#ruserpass");
    var userpass = document.querySelector("#userpass");
    username.addEventListener("blur", function () {
        if (regusername.test(username.value)) {
            console.log(username.value);
            nousername.style.display = "none";
            return true;
        } else {
            nousername.style.display = "block";
            return false;
        }
    });
    userpass.addEventListener("blur", function () {
        if (reguserpass.test(userpass.value)) {
            console.log(userpass.value);
            nouserpass.style.display = "none";
            return true;
        } else {
            nouserpass.style.display = "block";
            return false;
        }
    })
    ruserpass.addEventListener("blur", function () {
        if (ruserpass.value == userpass.value) {
            console.log(ruserpass.value);
            noruserpass.style.display = "none";
            return true;
        } else {
            noruserpass.style.display = "block";
            return false;
        }
    })
    window.addEventListener("mousemove", function () {
        if ( username.value == "" || userpass.value == "" || ruserpass.value == "" ) {
            //禁用按钮
            btn.disabled = true;
        }
        else {
            //启用按钮
            btn.disabled = false;
         }   
    })
    let ipts = document.querySelectorAll("input")
    btn.addEventListener("click", function (evt) {
        let username = ipts[0].value;
        let password = ipts[1].value;
        //发起注册请求
        let regAPI = `http://jx.xuzhixiang.top/ap/api/reg.php`
       let p = axios.get(regAPI,{params:{username,password}});
       p.then(r=>{
           //成功
           console.log(r.data);
           if (r.data.code == 0) {
               console.log(r.data);
              alert( r.data.msg + "请直接登录");
              location.href = "login.html"
           }else if(r.data.code == 1){
               alert(r.data.msg);
               //跳转到首页面
               location.href = "index.html"
           }
       })

        evt.preventDefault();
    })
})